import Vue from 'vue'

import VueRouter from "vue-router";

Vue.use(VueRouter)

//手写push
// 获取原始的push
const oldPush = VueRouter.prototype.push;
////手写push覆盖掉原始的,给原型上新增覆盖
VueRouter.prototype.push = function (
    //接收3个参数
    location,
    B=()=>{},
    C=()=>{},
){
    // 返回是原始push,只不过给了它后面两个参数一个初始值,如果为undefind就会返回失败的promise
    return oldPush.call(this,location,B,C)
}

//路由懒加载引入
const Home = ()=>import('@pages/Home')
const Search = ()=>import('@pages/Search')
const Login = ()=>import('@pages/Login')
const Register = ()=>import('@pages/Register')
const Detail = ()=>import('@pages/Detail')
const AddCartSuccess = ()=>import('@pages/AddCartSuccess')
const ShopCart = ()=>import('@pages/ShopCart')

export default new VueRouter({
    //设置滚动条。默认还原
    scrollBehavior(){
        return{
            x:0,
            y:0
        }
    },
    mode:'history',
    routes:[
        {
            path:'/home',
            component:Home,
            name:'home'
        },
        {
            path:'/search/:keyword?',
            component:Search,
            name:'search'
        },
        {
            path:'/login',
            component:Login,
            name:'login',
            meta:{
                isShow:true
            }
        },
        {
            path:'/register',
            component:Register,
            name:'register',
            meta:{
                isShow:true 
            }
        },
        {
            path:'/Detail/:skuId',
            component:Detail,
            name:'Detail',
        },
        {
            path:'/addcartsuccess',
            component:AddCartSuccess,
            name:'addcartsuccess',
        },
        {
            path:'/shopcart',
            component:ShopCart,
            name:'shopcart',
        },
        {
            path:'/',
            redirect:'/home'
        }
    ]
})